package hrms.controller;

import hrms.entity.Admin;
import hrms.service.AmqServiceImpl;
import hrms.util.LogUtil;
import hrms.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/amq")
public class AmqController {
	
	@Resource(name="amqService")
	private AmqServiceImpl amqService;
	
	@ExceptionHandler
	public String exHandle(Exception ex, HttpServletRequest req) {
		LogUtil.saveEx(ex);
		return "error";
	}
	
	@ResponseBody
	@RequestMapping("/sendText.sls")
	public Result<Object> executeA(HttpServletRequest req ,HttpServletResponse res){
		//前台获取到的消息
		String mes = req.getParameter("text");
		Admin admin = (Admin)req.getSession().getAttribute("admin");
		//session中存储的当前用户登录名
		String disname = admin.getAdminCode();
		System.out.println(disname+":"+mes);
		return amqService.sendToAmq(disname,mes);
	}
	
	@ResponseBody
	@RequestMapping("/recText.sls")
	public Result<Object> executeB(HttpServletRequest req ,HttpServletResponse res){
		//前台获取到的消息
		Admin admin = (Admin)req.getSession().getAttribute("admin");
		//session中存储的当前用户登录名
		String disname = admin.getAdminCode();
		Result<Object> r = amqService.recFromAmq(disname);
		return r;
	}
	
	@ResponseBody
	@RequestMapping("/recAll.sls")
	public Result<Object> executeC(HttpServletRequest req ,HttpServletResponse res){
		//前台获取到的消息
		Admin admin = (Admin)req.getSession().getAttribute("admin");
		//session中存储的当前用户登录名
		String disname = admin.getAdminCode();
		Result<Object> r = amqService.recAll(disname);
		System.out.println(r);
		return r;
	}
	
	@ResponseBody
	@RequestMapping("/sendTopic.sls")
	public Result<String> executeD(HttpServletRequest req ,HttpServletResponse res){
		String text = req.getParameter("text");
		Admin admin = (Admin)req.getSession().getAttribute("admin");
		String adminCode = admin.getAdminCode();
		Result<String> result = amqService.sendTopic(text);
		System.out.println(adminCode+"sendTopic:"+text);
		return result;
	}
	
	@ResponseBody
	@RequestMapping("/recTopic.sls")
	public Result<String> executeE(HttpServletRequest req ,HttpServletResponse res){
		Admin admin = (Admin)req.getSession().getAttribute("admin");
		String adminCode = admin.getAdminCode();
		Result<String> r = amqService.recTopic(adminCode);
		return r;
	}
}